package com.weikuang.fareast.activityNew;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.licheedev.myutils.LogPlus;
import com.tencent.bugly.crashreport.CrashReport;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.weikuang.fareast.R;
import com.weikuang.fareast.activity.BaseActivity;
import com.weikuang.fareast.bean.AddOrderOutBean;
import com.weikuang.fareast.bean.ChangListBean;
import com.weikuang.fareast.bean.ChangeHkBean;
import com.weikuang.fareast.bean.OrderBean;
import com.weikuang.fareast.bean.OutGoodsBean;
import com.weikuang.fareast.network.KillerApi;
import com.weikuang.fareast.network.MainApi;
import com.weikuang.fareast.network.MyObserve;
import com.weikuang.fareast.utils.UserHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.commonlibrary.ui.widget.CommonItemView;
import cn.dlc.commonlibrary.ui.widget.TitleBar;
import cn.dlc.commonlibrary.utils.TimeFormats;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HomeMainActivity extends BaseActivity implements OnDateSetListener {
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;
    @BindView(R.id.query)
    TextView query;
    @BindView(R.id.add)
    TextView add;
    @BindView(R.id.scrap)
    TextView scrap;

    @BindView(R.id.tv_money)
    TextView tv_money;


    @BindView(R.id.start_time)
    CommonItemView startTime;

    @BindView(R.id.end_time)
    CommonItemView endTime;

    @BindView(R.id.ll_now_mon)
    LinearLayout ll_now_mon;
    @BindView(R.id.iv_now_mon)
    ImageView iv_now_mon;

    @BindView(R.id.ll_last_mon)
    LinearLayout ll_last_mon;
    @BindView(R.id.iv_last_mon)
    ImageView iv_last_mon;
    @BindView(R.id.btn_change)
    TextView btn_change;


    private int type = 2;
    private int Timetype = 2;
    TimePickerDialog mDialogYearMonthDay;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_main);
        ButterKnife.bind(this);
        mDialogYearMonthDay = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH_DAY)
                .setCallBack(this)
                .build();

        iv_now_mon.setSelected(false);
        iv_last_mon.setSelected(true);
        if (UserHelper.get().getLevel() == 1){
            btn_change.setVisibility(View.VISIBLE);
        }else {
            btn_change.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        checkQuery();
    }



    @OnClick({R.id.query, R.id.add, R.id.scrap,R.id.start_time, R.id.end_time,R.id.ll_now_mon,R.id.iv_last_mon,R.id.tv_back,R.id.btn_change})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.query:
                startActivity(QueryhkActivity.class);
                break;
            case R.id.add:
                startActivity(AddListHkActivity.class);
                break;
            case R.id.scrap:
                startActivity(OutGoodsHkActivity.class);

               // startActivity(OrderDetalisHkActivity.class);
                break;
            case R.id.ll_now_mon:
                type = 1;
                iv_now_mon.setSelected(true);
                iv_last_mon.setSelected(false);
                checkQuery();
                break;
            case R.id.iv_last_mon:
                type = 2;
                iv_now_mon.setSelected(false);
                iv_last_mon.setSelected(true);
                checkQuery();
                break;
            case R.id.start_time:
                type = 0;
                Timetype = 1;
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");
                iv_now_mon.setSelected(false);
                iv_last_mon.setSelected(false);
                break;
            case R.id.end_time:
                type = 0;
                Timetype = 2;
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");
                iv_now_mon.setSelected(false);
                iv_last_mon.setSelected(false);
                break;
            case R.id.tv_back:
                finishActivity();
                break;
            case R.id.btn_change:
                startActivity(DataChangActivity.class);
                break;
        }
    }

    private void checkQuery() {
        String monthFlag = "";
        if (type == 1){
            monthFlag = "currentMonth";
        }else if (type == 2){
            monthFlag = "lastMonth";
        }else {
            monthFlag = "";
        }
        String start = startTime.getContentText();
        if (TextUtils.isEmpty(start)|| start.contains("请")){
            start = "";
        }
        String end = endTime.getContentText();
        if (end.contains("请")){
            end = "";
        }

       // showWaitingDialog("请等待",true);

        tv_money.setText("");
        MainApi.getInstance().getApi( KillerApi.class)
                .querySalary(UserHelper.get().getUserid(),monthFlag,start,end)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new MyObserve<OrderBean>() {
                    @Override
                    public void onNetError(Throwable e) {
                        LogPlus.e("e.getMessage()"+e.getLocalizedMessage());
                        showOneToast(getString(R.string.order_34));
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        LogPlus.e("onSubscribe");
                    }

                    @Override
                    public void onNext(OrderBean orderBean) {
                        LogPlus.e("onNext");
                        //dismissWaitingDialog();
                        tv_money.setText(orderBean.getData());

                    }
                });
    }
    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        if (Timetype == 1) {
            startTime.setContent(TimeFormats.formatDateTime1(timePickerView.getCurrentMillSeconds()));
        } else {
            endTime.setContent(TimeFormats.formatDateTime1(timePickerView.getCurrentMillSeconds()));
            checkQuery();
        }
    }
}
